package com.dbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dbms.domain.dto.BisXajmodelArearateDTO;
import com.dbms.domain.entity.BisXajmodelArearate;
import com.dbms.domain.entity.FloodmapStationPropagate;
import com.dbms.domain.vo.BisXajmodelArearateVO;
import com.dbms.mapper.BisXajmodelArearateMapper;
import com.dbms.service.BisXajmodelArearateService;
import com.dbms.service.FloodmapStationPropagateService;
import com.dbms.utils.SnowflakeIdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class BisXajmodelArearateServiceImpl implements BisXajmodelArearateService {

    final BisXajmodelArearateMapper mapper;
    final FloodmapStationPropagateService floodmapStationPropagateService;
    final SnowflakeIdGenerator snowflakeIdGenerator;


    @Override
    public boolean saveOrUpdate(BisXajmodelArearateDTO dto) {
        if(StringUtils.isBlank(dto.getId())){
            if(mapper.selectCount(new LambdaQueryWrapper<BisXajmodelArearate>().eq(BisXajmodelArearate::getStcdPn,dto.getStcdPn()))>0){
                throw new RuntimeException("雨量站不能重复");
            }
            BisXajmodelArearate arearate = new BisXajmodelArearate();
            BeanUtils.copyProperties(dto,arearate);
            FloodmapStationPropagate propagate = floodmapStationPropagateService.getByStationCod(dto.getStcdR());
            arearate.setStcdRn(propagate.getStationName());
            arearate.setId(snowflakeIdGenerator.nextId().toString());
            mapper.insert(arearate);
        }else {
            BisXajmodelArearate arearate = mapper.selectById(dto.getId());
            BeanUtils.copyProperties(dto,arearate);
            mapper.updateById(arearate);
        }
        return Boolean.TRUE;
    }

    @Override
    public boolean deleteByIds(List<String> ids) {
        mapper.deleteBatchIds(ids);
        return Boolean.TRUE;
    }

    @Override
    public List<BisXajmodelArearateVO> queryByStcdRToList(String stcdR) {
        List<BisXajmodelArearate> list = mapper.selectList(new LambdaQueryWrapper<BisXajmodelArearate>()
                .eq(BisXajmodelArearate::getStcdR,stcdR)
                .orderByDesc(BisXajmodelArearate::getNum)
        );
        List<BisXajmodelArearateVO> result=new ArrayList<>(list.size());
        for(BisXajmodelArearate arearate:list){
            BisXajmodelArearateVO vo=new BisXajmodelArearateVO();
            BeanUtils.copyProperties(arearate,vo);
            result.add(vo);
        }
        return result;
    }

    @Override
    public List<BisXajmodelArearate> queryByStcdR(String stcdR) {
        return mapper.selectList(new LambdaQueryWrapper<BisXajmodelArearate>()
                .eq(BisXajmodelArearate::getStcdR,stcdR)
        );
    }

    @Override
    public BisXajmodelArearate queryByStcdP(String stcdP) {
        return mapper.selectOne(new LambdaQueryWrapper<BisXajmodelArearate>()
                .eq(BisXajmodelArearate::getStcdP,stcdP)
        );
    }
}
